package my.hnb.android.controller;

import my.hnb.android.common.ActionEvent;
import my.hnb.android.common.ModelEvent;
import my.hnb.android.constant.ErrorConstants;
import my.hnb.android.constant.EventConstant;
import my.hnb.android.model.UserModel;
import my.hnb.android.network.HTTPRequest;
import my.hnb.android.view.base.BaseActivity;
import my.hnb.android.view.base.BaseFragment;
import my.hnb.android.view.karaoke.About;
import my.hnb.android.view.karaoke.ListSongsView;
import my.hnb.android.view.karaoke.SettingView;
import my.hnb.android.view.karaoke.SongDetailView;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

public class UserController extends AbstractController {
	static UserController instance;

	protected UserController() {
	}

	public static UserController getInstance() {
		if (instance == null) {
			instance = new UserController();
		}
		return instance;
	}

	@Override
	public void handleSwitchActivity(ActionEvent e) {
		Activity base = (Activity) e.sender;
		Intent intent;
		Bundle extras;
		switch (e.action) {
		case EventConstant.GOTO_KARAOKE_LIST: {
			intent = new Intent(base, ListSongsView.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		case EventConstant.GOTO_LIST_SONG_DETAIL: {
			intent = new Intent(base, SongDetailView.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		case EventConstant.GOTO_SETTING: {
			intent = new Intent(base, SettingView.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		case EventConstant.GOTO_ABOUT: {
			intent = new Intent(base, About.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		}
	}

	@Override
	public void handleViewEvent(final ActionEvent e) {
		AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
			protected Void doInBackground(Void... params) {
				switch (e.action) {
				case EventConstant.GET_SONGS:
				case EventConstant.SEARCH_SONG:
				case EventConstant.SEARCH_SONG_FAVORITE:
				case EventConstant.GET_SONGS_FAVORITE: {
					UserModel.getInstance().requestGetListSong(e);
					break;
				}
				case EventConstant.GET_SONG_DETAIL: {
					UserModel.getInstance().requestGetSongDetail(e);
					break;
				}
				case EventConstant.UPDATE_FAVORITE: {
					UserModel.getInstance().requestUpdateFavorite(e);
					break;
				}
				}
				return null;
			}
		};
		task.execute();
	}

	@Override
	public void handleModelEvent(final ModelEvent modelEvent) {
		final ActionEvent e = modelEvent.getActionEvent();
		// BaseActivity ac = (BaseActivity) e.sender;
		// if (ac.isFinished) {
		// return;
		// }
		//
		// ac.runOnUiThread(new Runnable() {
		// public void run() {
		// // TODO Auto-generated method stub
		// BaseActivity base = (BaseActivity) e.sender;
		// base.handleModelViewEvent(modelEvent);
		// }
		// });

		if (modelEvent.getModelCode() == ErrorConstants.ERROR_CODE_SUCCESS) {
			HTTPRequest request = e.request;
			if (e.sender != null && (request == null || (request != null && request.isAlive()))) {
				if (e.sender instanceof BaseActivity) {
					final BaseActivity sender = (BaseActivity) e.sender;
					if (sender.isFinished)
						return;
					sender.runOnUiThread(new Runnable() {
						public void run() {
							// TODO Auto-generated method stub
							sender.handleModelViewEvent(modelEvent);
						}
					});
				} else if (e.sender instanceof BaseFragment) {
					final BaseFragment sender = (BaseFragment) e.sender;
					if (sender.getActivity() == null || sender.isFinished) {
						return;
					}
					sender.getActivity().runOnUiThread(new Runnable() {
						public void run() {
							// TODO Auto-generated method stub
							sender.handleModelViewEvent(modelEvent);
						}
					});
				}
			} else {
				handleErrorModelEvent(modelEvent);
			}
		} else {
			handleErrorModelEvent(modelEvent);
		}

	}

	@Override
	public void handleErrorModelEvent(final ModelEvent modelEvent) {
		// TODO Auto-generated method stub
		handleCommonError(modelEvent);
		final ActionEvent e = modelEvent.getActionEvent();
		BaseActivity ac = (BaseActivity) e.sender;
		ac.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				BaseActivity base = (BaseActivity) e.sender;
				base.handleErrorModelViewEvent(modelEvent);
			}
		});
	}

	@Override
	public void handleSwitchFragment(ActionEvent e) {

	}

}
